package kr.or.kiso.test;

/**
 * MailService
 * 
 * @author hisong
 * @since 2011.10.27
 *
 * Copyright (C) 2011 by hisong All rights reserved.
 */

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import kr.or.kiso.common.Globals;

public class Mail {
	
	static String host ="smtp.gmail.com";//smtp 서버
	static String id = "thdgpdls013@gmail.com";
	static String passwd = "hisong0714";
	static String name = "관리자";
	
	public static void sendMail(String from, String to, String subject,  String content, String[] aryFile) {
		
		try {

			// 프로퍼티 값 인스턴스 생성과 기본세션(SMTP 서버 호스트 지정)
			Properties props = new Properties();
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.starttls.enable","true");
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.port", "465");
			props.put("mail.smtp.auth", "true");
			props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

			MyAuthenticator auth = new MyAuthenticator(id, passwd);
			
			Session sess = Session.getInstance(props,auth); // 인증이 필요 할 때
//			Session sess = Session.getInstance(props,null);	// 인증이 필요 없을 때

			MimeMessage msg = new MimeMessage(sess);
			msg.setFrom(new InternetAddress(from, name));//보내는 사람 설정
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);//받는 사람설정
			
			
			Multipart mp = new MimeMultipart();
			
			
			// create and fill the first message part
		    MimeBodyPart mbp1 = new MimeBodyPart();
		    mbp1.setText(content);
		    mp.addBodyPart(mbp1);

		    // create the second message part
		    MimeBodyPart mbp2 = new MimeBodyPart();
		    
			
			for(int i = 0 ; i < aryFile.length; i++) {
				String file_name = aryFile[i];
				
				DataSource source = new FileDataSource(Globals.FILE_PATH + File.separator +file_name);
		    	mbp2.setDataHandler(new DataHandler(source));
		    	mbp2.setFileName(file_name); 
		    	System.out.println("###보낼파일 : " + file_name);
		    	
			    mp.addBodyPart(mbp2);
				
			}
			    
		    // add the Multipart to the message
		    msg.setContent(mp, "text/html;charset=euc-kr");
			msg.setSubject(subject);//제목 설정
			msg.setSentDate(new java.util.Date());//보내는 날짜 설정
			
			System.out.println("Mail: from>" + from);
			System.out.println("Mail: to>" + to);
			System.out.println("Mail: body>" + content);
	
			
			Transport.send(msg);//메일 보내기
			

			System.out.println("ok");

		} catch (MessagingException ex) {
			System.out.println("mail send error : " + ex.getMessage());
			System.out.println("fail");

		} catch (Exception e) {
			System.out.println("error : " + e.getMessage());
			System.out.println("fail");
		}

	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Mail.sendMail("thdgpdls@gmail.com", "thdgpdls013@naver.com", "테스트 메일입니다.", "잘가니?", new String[]{});
	}

}
